# -*- coding: utf-8 -*-

import pymongo
from bson.objectid import ObjectId

def connect_mongodb():

    mongo = pymongo.MongoClient(host="127.0.0.1", port=27017)
    dbns = mongo.list_database_names()
    # dbs = mongo.list_databases()
    print(dbns)

    db = mongo.get_database(name="python")
    # db = mongodb.py_test
    print(type(db))

    cs = db.list_collection_names()
    print(cs)
    # db.list_collections()
    c = db.get_collection(name="mongo")
    print(type(c))
    ds = c.find()
    print(type(ds))
    for d in ds:
        # print(type(d))
        print(d)
        pass
    print(c.find_one(filter={"name": "hr"}))
    print(c.find_one(filter={"name": "gj"}))
    print(c.find_one(filter={"_id": ObjectId("5e4f50e8b7731c15543b5759")}))

    d1 = {"name": "hr", "gender": 0}
    print(type(d1))
    print(c.insert_one(document=d1).inserted_id)


    for d in c.find():
        # print(type(d))
        print(d)
        pass
    pass


if __name__ == '__main__':

    connect_mongodb()
    
    pass
